package com.system.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.web.controller.BaseController;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.system.service.ISysMenuService;
import com.system.util.MenuAddChildrenUtil;
import com.system.vo.MenuOperList;
import com.system.vo.SysMenuVO;

@Controller
@RequestMapping("/sys/menuMgr")
public class SysMenuController extends BaseController {

	@Resource
	private ISysMenuService menuService;

	@RequestMapping("/list")
	@ResponseBody
	public String list(HttpServletRequest request, HttpServletResponse response) {
		List<SysMenuVO> list = menuService.findAllMenu();
		List<SysMenuVO> menutreeList = MenuAddChildrenUtil.toTree(list);
		ObjectMapper om = new ObjectMapper();
		String menujson = null;

		try {
			menujson = om.writeValueAsString(menutreeList);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return menujson;
	}

	@RequestMapping
	public String tomenuManage() {
		return "view-system/menuManage/menuManage";
	}

	@RequestMapping("/createMenu")
	@ResponseBody
	public String createMenus(HttpServletRequest request, HttpServletResponse response) {
		String json = request.getParameter("queue");
		ObjectMapper om = new ObjectMapper();
		MenuOperList menuOperList = null;
		try {
			menuOperList = om.readValue(json, MenuOperList.class);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		List<SysMenuVO> sysMenuVOs = menuOperList.getAddNodes();
		if (sysMenuVOs != null && sysMenuVOs.size() > 0) {
			for (int i = 0; i < sysMenuVOs.size(); i++) {
				if(sysMenuVOs.get(i).getParMenuId()!=null&&sysMenuVOs.get(i).getParMenuId().isEmpty()){
					sysMenuVOs.get(i).setParMenuId(null);
				}
			}
			menuService.createMenu(sysMenuVOs);// 创建菜单功能
		}
		List<SysMenuVO> sysupdateMenuVOs = menuOperList.getUpdateNodes();
		if (sysupdateMenuVOs != null && sysupdateMenuVOs.size() > 0) {
			for (int i = 0; i < sysupdateMenuVOs.size(); i++) {
				if(sysupdateMenuVOs.get(i).getParMenuId()!=null&&sysupdateMenuVOs.get(i).getParMenuId().isEmpty()){
					sysupdateMenuVOs.get(i).setParMenuId(null);
				}
			}
			menuService.updateMenus(sysupdateMenuVOs);
		}
		List<SysMenuVO> sysdelMenuVOs = menuOperList.getDelNodes();
		if (sysdelMenuVOs != null && sysdelMenuVOs.size() > 0) {
			List<String> menuIDs = new ArrayList<String>();
			for (SysMenuVO sysMenuVO : sysdelMenuVOs) {
				menuIDs.add(sysMenuVO.getMenuId());
			}
			List<SysMenuVO> nextNodes = new ArrayList<SysMenuVO>();// ID子节点包括自己
			nextNodes = menuService.findAllChildrenMenu(menuIDs);

			for (SysMenuVO sysMenuVO : nextNodes) {
				menuService.deleteRoleAndMenuByID(sysMenuVO.getMenuId());
			}
			menuService.deleteMenuById(nextNodes);
		}
		return toJSONString("");
	}

	// 修改菜单内容
	@RequestMapping("/updateMenu")
	public void updateMenus(HttpServletRequest request, HttpServletResponse response) {
		String json = request.getParameter("queue");
		ObjectMapper om = new ObjectMapper();
		MenuOperList menuOperList = null;
		try {
			menuOperList = om.readValue(json, MenuOperList.class);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		List<SysMenuVO> sysMenuVOs = menuOperList.getUpdateNodes();
		for (int i = 0; i < sysMenuVOs.size(); i++) {
			if(sysMenuVOs.get(i).getParMenuId()!=null&&sysMenuVOs.get(i).getParMenuId().isEmpty()){
				sysMenuVOs.get(i).setParMenuId(null);
			}
		}
		menuService.updateMenus(sysMenuVOs);
	}

}
